package com.kkb.cubemall.product.demo;

import java.util.HashMap;

/**
 * @ClassName MyList
 * @Description
 * @Author hubin
 * @Date 2021/7/15 17:26
 * @Version V1.0
 **/
public class MyList {

        public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
            ListNode current1 = pHead1;
            ListNode current2 = pHead2;


            HashMap<ListNode, Integer> hashMap = new HashMap<ListNode, Integer>();
            // 先走第一個長度的鏈錶
            while (current1 != null) {
                hashMap.put(current1, null);
                current1 = current1.next;
            }
            // 再走第二長度的鏈錶，使用hashmap判斷是否存在相同的key,如果存在，返回即可
            while (current2 != null) {
                if (hashMap.containsKey(current2))
                    return current2;
                current2 = current2.next;
            }

            return null;

        }

// 定以一個鏈錶
class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}

}


